//
//  HLRegisterViewController.m
//  HLAerospace
//
//  Created by hulian-ios on 17/8/30.
//  Copyright © 2017年 hulian-LB. All rights reserved.
//

#import "HLRegisterViewController.h"
#import "HLLoginTextFiled.h"
#import <ReactiveObjC/ReactiveObjC.h>
#import "HttpRequestManager.h"
#import "MBProgressHUD+HL.h"

@interface HLRegisterViewController ()

/** 用户名  */
@property (nonatomic ,strong) HLLoginTextFiled *userName;
/** 密码  */
@property (nonatomic ,strong) HLLoginTextFiled *userPwd;
/** 注册  */
@property (nonatomic ,strong) UIButton *registerButton;

@end

@implementation HLRegisterViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setUI];
    
    RAC(_registerButton,enabled) = [RACSignal combineLatest:@[_userName.rac_textSignal,_userPwd.rac_textSignal] reduce:^id _Nullable(NSString *phone,NSString *password){
        return @(phone.length > 0  && password.length > 0 );
    }];
}

- (void)setUI
{
    self.navigationItem.title = @"注册";
    CGFloat buttonH = IS_IPHONE_5 ? 35 : 40;
    CGFloat margin = IS_IPHONE_5 ? 15 : 20;
    self.userName = ({
        HLLoginTextFiled *t = [[HLLoginTextFiled alloc] init];
        CGFloat buttonH = IS_IPHONE_5 ? 35 : 40;
        t.layer.cornerRadius = buttonH * 0.5;
        t.clipsToBounds = YES;
        t.frame = CGRectMake(10, 84, ScreenWidth - margin, buttonH);
        t.placeholder = @"请输入用户名";
        [self.view addSubview:t];
        [t setValue:[UIFont systemFontOfSize:kMainTitleFontSize]forKeyPath:@"_placeholderLabel.font"];
        t;
    });
    
    self.userPwd = ({
        HLLoginTextFiled *t = [[HLLoginTextFiled alloc] init];
        CGFloat buttonH = IS_IPHONE_5 ? 35 : 40;
        t.layer.cornerRadius = buttonH * 0.5;
        t.clipsToBounds = YES;
        t.secureTextEntry = YES;
        t.clearsOnBeginEditing = YES;
        t.frame = CGRectMake(10, CGRectGetMaxY(self.userName.frame) + margin, ScreenWidth - 20, buttonH);
        t.placeholder = @"请输入密码";
        [self.view addSubview:t];
        [t setValue:[UIFont systemFontOfSize:kMainTitleFontSize]forKeyPath:@"_placeholderLabel.font"];
        t;
    });
    
    self.registerButton = ({
        UIButton *l = [UIButton buttonWithType:UIButtonTypeCustom];
        l.frame = CGRectMake(10, CGRectGetMaxY(self.userPwd.frame) + margin + 5, ScreenWidth - 20, buttonH + 4);
        l.layer.cornerRadius = (buttonH + 4 )* 0.5;
        l.clipsToBounds = YES;
        l.titleLabel.font = [UIFont systemFontOfSize:15];
        l.enabled = NO;
        [l setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [l setTitle:@"注册" forState:UIControlStateNormal];
        [l setBackgroundImage:[UIImage imageWithColor:kColorMineBlueBG] forState:UIControlStateNormal];
        [l setBackgroundImage:[UIImage imageWithColor:kColorMineDisableBlueBG] forState:UIControlStateDisabled];
        [l addTarget:self action:@selector(registerButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:l];
        l;
    });
    
}


- (void)registerRequest
{
    
}

- (void)registerButtonClick
{

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"sign"] = [kHLRequestRegister signStr];
    params[@"username"] = self.userName.text;
    params[@"password"] = [self.userPwd.text md5Str];
    
    [[HTTPRequestManager defaultManager] HTTPGetRequestWithApiName:kHLRequestRegister parameters:params success:^(NSURLSessionDataTask * _Nonnull dataTask, id  _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            [MBProgressHUD showSuccessMessage:@"注册成功"];
        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
        
    } failure:^(NSURLSessionDataTask * _Nonnull dataTask, NSError * _Nonnull error) {
        [MBProgressHUD showErrorMessage: @"网络连接失败"];
        
    }];

}




@end
